package com.specialized.passenger.service.impl;

import java.util.List;

import com.specialized.common.core.utils.AESUtils;
import com.specialized.common.core.utils.DateUtils;

import com.specialized.common.security.utils.SecurityUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import com.specialized.passenger.mapper.InviteMapper;
import com.specialized.passenger.domain.Invite;
import com.specialized.passenger.service.IInviteService;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
/**
 * 邀请记录Service业务层处理
 *
 * @author djh
 * @date 2024-07-21
 */
@Service
@Log4j2
public class InviteServiceImpl implements IInviteService {
    @Autowired
    private InviteMapper inviteMapper;

    @Autowired
    private JavaMailSender javaMailSender;



    public static final String KEY="12345678asdfghjk";


    /**
     * 查询邀请记录
     *
     * @param inviteId 邀请记录主键
     * @return 邀请记录
     */
    @Override
    public Invite selectInviteByInviteId(Long inviteId) {
        return inviteMapper.selectInviteByInviteId(inviteId);
    }

    /**
     * 查询邀请记录列表
     *
     * @param invite 邀请记录
     * @return 邀请记录
     */
    @Override
    public List<Invite> selectInviteList(Invite invite) {
        return inviteMapper.selectInviteList(invite);
    }

    /**
     * 新增邀请记录
     *
     * @param invite 邀请记录
     * @return 结果
     */
    @Override
    public int insertInvite(Invite invite) {
        invite.setCreateTime(DateUtils.getNowDate());
        int i = inviteMapper.insertInvite(invite);
        if (i>0){
            sendEmil(invite.getEmil(),invite.getCode());
        }
        return i;
    }

    private void sendEmil(String emil,String url) {

        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject("邀请注册");
            helper.setText("<a href="+url+">点击注册</a>",true);
            helper.setFrom("19913268730@163.com");
            helper.setTo(emil);
            javaMailSender.send(mimeMessage);
            log.info("向"+emil+"发送邀请邮件成功");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改邀请记录
     *
     * @param invite 邀请记录
     * @return 结果
     */
    @Override
    public int updateInvite(Invite invite) {
        return inviteMapper.updateInvite(invite);
    }

    /**
     * 批量删除邀请记录
     *
     * @param inviteIds 需要删除的邀请记录主键
     * @return 结果
     */
    @Override
    public int deleteInviteByInviteIds(Long[] inviteIds) {
        return inviteMapper.deleteInviteByInviteIds(inviteIds);
    }

    /**
     * 删除邀请记录信息
     *
     * @param inviteId 邀请记录主键
     * @return 结果
     */
    @Override
    public int deleteInviteByInviteId(Long inviteId) {
        return inviteMapper.deleteInviteByInviteId(inviteId);
    }


    /**
     * 生成邀请好友链接
     * @return
     */
    @Override
    public String createUrl() {
        Long userId = SecurityUtils.getUserId();
        String encrypt = AESUtils.encrypt(userId.toString(), KEY);
        log.info(encrypt);
        String url="http://127.0.0.1/register?userId="+encrypt;
        return url;
    }
}
